package com.qf.common;

import com.qf.constant.CommonConstant;
import com.qf.pojo.entity.TesterDevice;
import com.qf.pojo.entity.TesterMode;
import com.qf.pojo.vo.User;
import com.qf.utils.ConfigUtils;
import com.qf.utils.CustomException;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;

import com.qf.utils.CustomException;

/**
 * @创建人: fengli
 * @创建时间: 2018/11/7 0007 17:52
 * @包: com.qf.common
 * @描述:
 */
public class SGHelper {

    private static Logger logger = Logger.getLogger(SGHelper.class);
    //
    //-------------登入鉴权及展示接口----------------------------------------------------------------------------------------------------------
    //

    /**
     * //AA_1：用户登录接口
     *
     * @return
     */
    public static JSONObject login(User user) throws CustomException {
        JSONObject map = new JSONObject();
        map.put("testerName", user.getTestName());
        map.put("testerPassword", user.getTesterPassword());
        logger.info(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        System.out.println(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        String result = "";
        result = HttpClientUtil.doPostForJson(CommonConstant.login, map);
        logger.info(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        System.out.println(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        return JSONObject.fromObject(result);
    }

    /**
     * //AA_2：根据用户登录信息,查询用户下的所有租户
     *
     * @param testerId
     * @return
     */
    public static JSONObject getTenantFromUser(String testerId) throws CustomException {
        JSONObject map = new JSONObject();
        map.put("testerId", testerId);
        logger.info(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        System.out.println(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        String result = "";
        result = HttpClientUtil.doPostForJson(CommonConstant.getTenantFromUser, map);
        logger.info(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        System.out.println(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        return JSONObject.fromObject(result);
    }

    /**
     * /注册时获取验证码接口—(石尧)
     *
     * @return
     */
    public static JSONObject getVerificationCode(String mailBox) throws CustomException {
        JSONObject map = new JSONObject();
        map.put("mailBox", mailBox);
        logger.info(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        System.out.println(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        String result = "";
        result = HttpClientUtil.doPostForJson(CommonConstant.getVerificationCode, map);
        logger.info(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        System.out.println(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        return JSONObject.fromObject(result);
    }

    /**
     * 接口AA_8：注册接口—(石尧)注册成功后返回用户ID
     *
     * @return
     */
    public static JSONObject userRegister(User user) throws CustomException {
        JSONObject map = new JSONObject();
        map.put("testName", user.getTestName());
        map.put("testPassword", user.getTesterPassword());
        map.put("mailBox", user.getMailBox());
        map.put("verificationCode", user.getVerificationCode());
        logger.info(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        System.out.println(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        String result = "";
        result = HttpClientUtil.doPostForJson(CommonConstant.userRegister, map);
        System.out.println(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        return JSONObject.fromObject(result);
    }

    /**
     * 重置密码时获取邮箱验证码
     *
     * @param testName
     * @return
     */
    public static JSONObject resetPasswords(String testName) throws CustomException {
        JSONObject map = new JSONObject();
        map.put("testName", testName);
        logger.info(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        System.out.println(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        String result = "";
        result = HttpClientUtil.doPostForJson(CommonConstant.resetPasswords, map);
        logger.info(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        System.out.println(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        return JSONObject.fromObject(result);
    }

    /**
     * AA_3：根据所选租户id,查询该租户下的所有产品
     *
     * @param tenantID
     * @return
     */
    public static String getAllProductsNameId(String tenantID) throws CustomException {
        logger.info(ConfigUtils.getTraceInfo() + "-----------in:" + tenantID);
        System.out.println(ConfigUtils.getTraceInfo() + "-----------in:" + tenantID);
        String result = "";
        result = HttpClientUtil.doGet(CommonConstant.getAllProductsNameId + "?tenantID=" + tenantID + "&pageNum=1&pageSize=10000", null);
        logger.info(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        System.out.println(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        return result;
    }

    /**
     * AA_4：校验模块ID合法性,对每个boxId进行校验,验证是否属于上面选择的产品(POST)
     *
     * @param testerId
     * @param tenantId
     * @param boxId
     * @param productId
     * @return
     */
    public static JSONObject boxIdValidation(String testerId, String tenantId, String boxId, String productId) throws
            CustomException {
        JSONObject map = new JSONObject();
        map.put("testerId", testerId);
        map.put("tenantId", tenantId);
        map.put("boxId", boxId);
        map.put("productId", productId);
        logger.info(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        String result = "";
        result = HttpClientUtil.doPostForJson(CommonConstant.boxIdValidation, map);
        logger.info(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        System.out.println(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        return JSONObject.fromObject(result);
    }


    /**
     * 接口AA_11 : 重置密码接口--administrator
     *
     * @return
     */
    public static JSONObject modificationUserPassWord(User user) throws CustomException {
        JSONObject map = new JSONObject();
        map.put("testName", user.getTestName());
        map.put("verificationCode", user.getVerificationCode());
        map.put("newPassword", user.getNewPassword());
        logger.info(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        System.out.println(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        String result = "";
        result = HttpClientUtil.doPostForJson(CommonConstant.modificationUserPassWord, map);
        logger.info(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        System.out.println(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        return JSONObject.fromObject(result);
    }

    /**
     * 点击开始,将页面所有boxId传到后台
     *
     * @return
     */
    public static JSONObject startTest(String testerId, String tenantId, String productId, String ids) throws
            CustomException {
        String[] split = ids.split(",");
        JSONObject map = new JSONObject();
        map.put("testerId", testerId);
        map.put("tenantId", tenantId);
        map.put("productId", productId);
        JSONArray boxArray = new JSONArray();
        for (String s : split) boxArray.add(s);
        map.put("boxIdList", boxArray);
        logger.info(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        String result = "";
        result = HttpClientUtil.doPostForJson(CommonConstant.startTest, map);
        logger.info(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        System.out.println(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        return JSONObject.fromObject(result);
    }


    /**
     * 停止测试接口
     *
     * @return
     */
    public static JSONObject ToStopTesting(String ids) throws CustomException {
        String[] split = ids.split(",");
        JSONObject map = new JSONObject();
        JSONObject boxId = new JSONObject();
        JSONArray boxArray = new JSONArray();
        for (String s : split) {
            boxId.put("boxId", s);
            boxArray.add(boxId);
        }
        map.put("listData", boxArray);
        logger.info(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        System.out.println(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        String result = "";
        result = HttpClientUtil.doPostForJson(CommonConstant.ToStopTesting, map);
        logger.info(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        System.out.println(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        return JSONObject.fromObject(result);
    }

    /**
     * 获取云盒对应的设备最新工况信息接口
     *
     * @param boxId
     * @param tenantId
     * @return
     */
    public static String getDeviceStatusToMtms(String boxId, String tenantId) throws CustomException {
        JSONObject map = new JSONObject();
        map.put("boxId", boxId);
        map.put("tenantId", tenantId);
        logger.info(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        System.out.println(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        String result = "";
        result = HttpClientUtil.doPostForJson(CommonConstant.getDeviceStatusToMtms, map);
        logger.info(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        System.out.println(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        return result;
    }

    /**
     * 根据测试人员id,获取boxId和其对应的状态
     *
     * @param testerMode
     * @return
     */
    public static JSONObject getBoxIdAndTestResult(TesterMode testerMode) throws CustomException {
        JSONObject map = new JSONObject();
        map.put("testerId", testerMode.getTestUserId());
        map.put("testTenantId", testerMode.getTestTenantId());
        map.put("produceId", testerMode.getProduceId());
        logger.info(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        System.out.println(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        String result = "";
        result = HttpClientUtil.doPostForJson(CommonConstant.getBoxIdAndTestResult, map);
        logger.info(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        System.out.println(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        return JSONObject.fromObject(result);
    }

    /**
     * 根据测试人员id,获取boxId和其对应的状态
     *
     * @param testerMode
     * @return
     */
    public static JSONObject getConfigById(TesterMode testerMode) throws CustomException {
        JSONObject map = new JSONObject();
        map.put("testerId", testerMode.getTestUserId());
        map.put("tenantId", testerMode.getTestTenantId());
        map.put("productId", testerMode.getProduceId());
        logger.info(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        System.out.println(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        String result = "";
        result = HttpClientUtil.doPostForJson(CommonConstant.getConfigById, map);
        logger.info(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        System.out.println(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        return JSONObject.fromObject(result);
    }

    /**
     * 根据boxId,清理KafkaData
     * @param ids
     * @return
     * @throws CustomException
     */
    public static JSONObject cleanBoxIdMap(String testerId,String testType,String ids) throws CustomException {
        JSONObject map = new JSONObject();
        String[] split =  ids.split(",");
        JSONArray jsonArray = JSONArray.fromObject(split);
        map.put("testerId", testerId);
        map.put("testType", testType);
        map.put("boxIdList", jsonArray);
        logger.info(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        System.out.println(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        String result = "";
        result = HttpClientUtil.doPostForJson(CommonConstant.cleanBoxIdMap, map);
        System.out.println(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        logger.info(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        return JSONObject.fromObject(result);
    }

    //
    //-------------测试配置信息的录入----------------------------------------------------------------------------------------------------------
    //

    public static JSONObject CheckRuleStore(TesterMode testerMode) throws CustomException {
        logger.info(ConfigUtils.getTraceInfo() + "-----------in:" + JSONObject.fromObject(testerMode));
        String result = "";
        JSONObject jsonObject = JSONObject.fromObject(testerMode);
        System.out.println(jsonObject);
        jsonObject.put("tenantId", testerMode.getTestTenantId());
        jsonObject.put("testerId", testerMode.getTestUserId());
        result = HttpClientUtil.doPostForJson(CommonConstant.CheckRuleStore, jsonObject);
        logger.info(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        System.out.println(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        return JSONObject.fromObject(result);
    }

    public static JSONObject TestPatternStore(TesterMode testerMode) throws CustomException {
        JSONObject map = new JSONObject();
        map.put("testUserId", testerMode.getTestUserId());
        map.put("testTenantId", testerMode.getTestTenantId());
        map.put("produceId", testerMode.getProduceId());
        map.put("produceName", testerMode.getProduceName());
        map.put("deviceTypeId", testerMode.getDeviceTypeId());

        String testPeriod = testerMode.getTestPeriod();
        if (StringUtils.isEmpty(testPeriod)) {
            testPeriod = "0";
        }
        String cmdInterval = testerMode.getCmdInterval();
        if (StringUtils.isEmpty(cmdInterval)) {
            cmdInterval = "0";
        }
        String successRadio = testerMode.getSuccessRadio();
        if (StringUtils.isEmpty(successRadio)) {
            successRadio = "0";
        }
        map.put("testPeriod", testPeriod);
        map.put("cmdInterval", cmdInterval);
        map.put("successRadio", successRadio);
        map.put("testType", testerMode.getTestType());
        logger.info(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        System.out.println(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        String result = "";
        result = HttpClientUtil.doPostForJson(CommonConstant.TestPatternStore, map);
        logger.info(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        System.out.println(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        return JSONObject.fromObject(result);
    }

    public static JSONObject getDeviceTypeContent(String deviceTypeID) throws CustomException {
        logger.info(ConfigUtils.getTraceInfo() + "-----------in:" + deviceTypeID);
        System.out.println(ConfigUtils.getTraceInfo() + "-----------in:" + deviceTypeID);
        String result = "";
        result = HttpClientUtil.doGet(CommonConstant.getDeviceTypeContent + "?deviceTypeID=" + deviceTypeID, null);
        logger.info(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        System.out.println(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        return JSONObject.fromObject(result);
    }

    public static JSONObject TestPatternQuery(TesterMode testerMode) throws CustomException {
        JSONObject map = new JSONObject();
        map.put("testUserId", testerMode.getTestUserId());
        map.put("testTenantId", testerMode.getTestTenantId());
        map.put("produceId", testerMode.getProduceId());
        logger.info(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        System.out.println(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        String result = "";
        result = HttpClientUtil.doPostForJson(CommonConstant.TestPatternQuery, map);
        logger.info(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        System.out.println(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        return JSONObject.fromObject(result);
    }

    public static JSONObject CheckRuleQuery(TesterMode testerMode) throws CustomException {
        JSONObject map = new JSONObject();
        map.put("testTenantId", testerMode.getTestTenantId());
        map.put("deviceTypeId", testerMode.getDeviceTypeId());
        logger.info(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        System.out.println(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        String result = "";
        result = HttpClientUtil.doPostForJson(CommonConstant.CheckRuleQuery, map);
        logger.info(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        result =  result.replaceAll("null", "\"null\"");
        System.out.println(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        JSONObject jsonObject = JSONObject.fromObject(result);
        return jsonObject;
    }


    //
    //-------------查询测试结果集----------------------------------------------------------------------------------------------------------
    //


    /**
     * 单次的执行结果和状态查询
     *
     * @param boxId
     * @return
     */
    public static JSONObject singleTestResultQuery(String boxId) throws CustomException {
        JSONObject map = new JSONObject();
        map.put("boxId", boxId);
        logger.info(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        System.out.println(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        String result = "";
        result = HttpClientUtil.doPostForJson(CommonConstant.singleTestResultQuery, map);
        logger.info(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        System.out.println(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        return JSONObject.fromObject(result);
    }

    /**
     * 获取压力测试的执行结果和失败原因统计表查询
     *
     * @return
     */
    public static JSONObject PressureTestResultsQuery(TesterDevice testerDevice) throws CustomException {
        JSONObject map = new JSONObject();
        map.put("testUserId", testerDevice.getTestUserId());
        map.put("testTenantId", testerDevice.getTestTenantId());
        map.put("produceId", testerDevice.getProductId());
        map.put("boxId", testerDevice.getBoxId());
        logger.info(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        System.out.println(ConfigUtils.getTraceInfo() + "-----------in:" + map.toString());
        String result = "";
        result = HttpClientUtil.doPostForJson(CommonConstant.PressureTestResultsQuery, map);
        result.replaceAll("null", "\"0\"");
        logger.info(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        System.out.println(ConfigUtils.getTraceInfo() + "-----------out:" + result);
        return JSONObject.fromObject(result);
    }


}
